import React, { Component } from 'react';
import LoginView from './LoginView';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import {Redirect} from "react-router-dom"
import * as userActions from '../../actions/user'


class Login extends Component {
    state={
        uname:""
    }
    loginHandler(user){
        // this.props.userActions.logining({
        //     user
        // });
        // setTimeout(()=>{
        //     this.props.userActions.loginSuccess();
        // },2000)
        this.props.login(user)
    }
    changeUname(name){
       this.setState({
           name
       })
    }
    componentDidUpdate(){
        if(this.props.isLogin){
            this.props.history.push('/home')
        }else{
           return
        }
    }
    render() {
        return (
            <div>
               <LoginView onLoginHandler={this.loginHandler.bind(this)} loading={this.props.loading} changeUname ={this.changeUname.bind(this)} error={this.props.error}/>
            </div>
        );
    }
}
const mapStateToProps = (state)=>{
    return{
        isLogin:state.user.isLogin,
        loading:state.user.loading,
        error:state.user.error
    }
}
// const mapDispatchToProps = (dispatch)=>{
//     return{
//         userActions:bindActionCreators(userActions,dispatch),
//     }
// }


export default connect(mapStateToProps,{login: (name)=>({type:"login",name})})(Login);//type:"login"对应的是saga take任务名